<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from '../../../dashboard/mixins/resize'

export default {
  mixins: [resize],
  props: {
    handlePercentOfficeLineChart:{
      type: Function,
      default: null
    },
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)
      var that = this;
      this.chart.on('click',function(params){
        var answer = params.seriesName;
        var number = params.name;
        var countValue = params.value;
        var index = params.dataIndex;
        var data = that.chartData.zdata[index];
        var params = that.$parent.addDateRange(that.$parent.queryParams,that.$parent.dateRange);
        params.deptId = data;
 　　　  that.handlePercentOfficeLineChart(params);//调用接口
      })
      
    },
    setOptions( {xdata,ydata,zdata,adata,bdata}  = {}) {
      this.chart.setOption({
        color: ['#3398DB'],
        tooltip: {
            trigger: 'axis',
            axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
            },
            formatter: function(params){
              var index = params[0].dataIndex;
              return params[0].axisValue+"："+params[0].value+"%</br>"
            }
        },
        title: {
            text: adata,
            textStyle: {
              fontSize: 15,
              color: "#00FFE3"
            },
            left: '6',
            top: "10"
        },
        grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
        },
        "xAxis": [{
            "type": "category",
            "axisLine": {
                lineStyle: {
                    color: '#4693EC'
                }
            },
            "splitLine": {
                "show": false
            },
            "axisTick": {
                "show": false
            },
            "splitArea": {
                "show": false
            },
            "axisLabel": {
                interval: 0,
                rotate:"45",
                color: "#00FFE3",
                fontSize:12
            },
            "data": xdata,
        }],
        "yAxis": [{
            "type": "value",
            "splitLine": {
                "show": false
            },
            "axisLine": {
                lineStyle: {
                    color: '#4693EC'
                }
            },
            "axisTick": {
                "show": false
            },
            "axisLabel": {
                interval: 0,
                rotate:"45",
                color: "#00FFE3",
                fontSize:12
            },
            "splitArea": {
                "show": false
            },

        }],
        series: [{
            type: "bar",
            barWidth: 26,
            label: {
                    show: true,
                    color: '#00FFE3',
                    position: 'top'
                },
            itemStyle: {
                normal: {
                    color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                            offset: 0,
                            color: "#00FFE3"
                        },
                        {
                            offset: 1,
                            color: "#4693EC"
                        }
                    ])
                }
            },
            data:ydata
        }]
     })
    }
  }
}
</script>
